﻿using BigEgg.Framework.Foundation;
using FMStudio.Documents.TemplateDocument.Models;
using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace FMStudio.Documents.TemplateDocument
{
    [XmlRoot("FMSTemplate")]
    public class TemplateDocument : Document
    {
        #region Fields
        private string formatVersion = "1.0.0";
        private TemplateType type;

        private TemplateInformation templateData;
        private TemplateWizard wizardExtension;
        private TemplateContent templateContent;
        #endregion


        public TemplateDocument(TemplateType type)
            : base(true)
        {
            this.type = type;

            this.templateData = new TemplateInformation();
            this.wizardExtension = null;
            if (this.type == TemplateType.Item)
            {
                this.templateContent = new ItemTemplateContent();
            }
            else
            {
                this.templateContent = new ProjectTemplateContent();
            }

            AddWeakEventListener(this.templateData, TemplateDataPropertyChanged);
            AddWeakEventListener(this.templateContent, TemplateContentChanged);
        }

        public TemplateDocument()
            : this(TemplateType.Project)
        {
        }


        #region Properties
        [XmlAttribute()]
        public string FormatVersion
        {
            get { return this.formatVersion; }
            set
            {
                this.formatVersion = value;
            }
        }

        [XmlAttribute()]
        public TemplateType Type
        {
            get { return this.type; }
            set
            {
                this.type = value;
            }
        }

        [XmlElement(IsNullable = false, Order = 1)]
        public TemplateInformation TemplateData 
        { 
            get { return this.templateData; }
            set
            {
                this.templateData = value;
            }
        }

        [XmlElement(Order = 3)]
        public TemplateWizard WizardExtension 
        { 
            get { return this.wizardExtension; } 
            set
            {
                if (this.wizardExtension != value)
                {
                    if (this.wizardExtension != null)
                        RemoveWeakEventListener(this.wizardExtension, WizardExtensionPropertyChanged);

                    this.wizardExtension = value;

                    if (this.wizardExtension != null)
                        AddWeakEventListener(this.wizardExtension, WizardExtensionPropertyChanged);
                    RaisePropertyChanged("WizardExtension");
                }
            }
        }

        [XmlElement(Order = 2)]
        [XmlElement(Type = typeof(ItemTemplateContent)),
         XmlElement(Type = typeof(ProjectTemplateContent))]
        public TemplateContent TemplateContent 
        { 
            get { return this.templateContent; } 
            set
            {
                this.templateContent = value;
            }
        }
        #endregion

        #region Methods
        #region Private Methods
        private void TemplateDataPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.IsModified = true;
            RaisePropertyChanged("TemplateData");
        }

        private void WizardExtensionPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.IsModified = true;
            RaisePropertyChanged("WizardExtension");
        }

        private void TemplateContentChanged(object sender, PropertyChangedEventArgs e)
        {
            this.IsModified = true;
        }
        #endregion
        #endregion
    }
}
